package leetcode.violence;

import java.util.Scanner;

public class 巫妖王 {
    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
        int money = scanner.nextInt();
        int[] soles = new int[3];
        for (int i = 0; i < soles.length; i++) {
            soles[i]=scanner.nextInt();
        }
        System.out.println(process(money,soles,0));
    }
    public static int process(int money,int[] arr,int k){
        if (arr.length==k){
            return money;
        }
        int res =Integer.MAX_VALUE;
        for (int i = 0; i*arr[k]<=money; i++) {
            int next = process(money-i*arr[k],arr,k+1);
            res =  Math.min(res,next);
        }
        return res;
    }

}
